<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <script src="lib/jquery-1.7.2.js"></script>
    <script src="lib/bootstrap.js"></script>
    <script src="vue.js"></script>
    <title>Title</title>
</head>
<body>
<div clas="container" id="box">
    <div class="col-sm-4">
        <form role="form">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" class="form-control" placeholder="输入用户名" v-model="username">
            </div>
            <div class="form-group">
                <label for="age">年龄:</label>
                <input type="text" id="age" class="form-control" placeholder="输入年龄" v-model="age">
            </div>
            <div class="form-group">
                <input type="button" value="添加" class="btn-primary" v-on:click="addInput">
                <input type="button" value="重置" class="btn-danger" v-on:click="resetInput">
            </div>
        </form>
        <hr>
        <table class="table table-bordered">
            <caption class="h2 text-danger">用户信息表</caption>
            <tr class="text-danger">
                <th class="text-center">序号</th>
                <th class="text-center">名字</th>
                <th class="text-center">年龄</th>
                <th class="text-center">操作</th>
            </tr>
            <tr class="text-center" v-for="(item,key) in myData">
                <td>{{key+1}}</td>
                <td>{{item.name}}</td>
                <td>{{item.age}}</td>
                <td><input type="button" value="删除" class="btn-danger btn btn-sm" data-toggle="modal"
                           data-target='#layer' v-on:click="nowIndex=key;msg='确认删除吗?'"></td>
            </tr>
            <tr class="text-right" v-show="myData.length != 0">
                <td colspan="4">
                    <button class="btn btn-danger" v-on:click="nowIndex=-2;msg='确认删除全部吗？'" data-toggle="modal" data-target="#layer">删除全部
                    </button>
                </td>
            </tr>
            <tr v-show="myData.length == 0">
                <td colspan="4" class="text-center text-muted">
                    <p>暂无数据....</p>
                </td>
            </tr>
        </table>
    </div>
    <div role="dialog" class="modal fade" id="layer">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                    <h4 class="modal-title">{{msg}}</h4>
                </div>
                <div class="modal-body text-right">
                    <button class="btn btn-primary btn-sm" data-dismiss="modal">取消</button>
                    <button class="btn btn-danger btn-sm" data-dismiss="modal" v-on:click="delOne(nowIndex)">确认</button>
                </div>
            </div>
        </div>

    </div>
</div>
</body>
<script>
    var app = new Vue({
        el: '#box',
        data: {
            myData: [],
            username: '',
            age: '',
            nowIndex: -100,
            msg:'确认删除吗?'
        },
        methods: {
            addInput: function () {
                this.myData.push({
                    name: this.username,
                    age: this.age
                });
                this.username = '';
                this.age = '';
            },
            resetInput: function () {
                this.username = '';
                this.age = '';
            },
            delOne: function (n) {
                if (n == -2) {
                    this.myData = [];
                } else {
                    this.myData.splice(this.nowIndex, 1);
                }
            }
        }
    })

</script>
</html>